package momanii.hani.supernova_emoji;

import hani.momanii.supernova_emoji_library.Actions.EmojIconActions;
import hani.momanii.supernova_emoji_library.Handler.EmojiconTextField;
import hani.momanii.supernova_emoji_library.Handler.EmojiconText;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

public class MainAbilitySlice extends AbilitySlice {
    private EmojiconTextField textField;
    private EmojiconTextField textField1;
    private EmojiconText text;
    private Text title;
    private Image emoji;
    private Image submit;
    private Checkbox checkBox;
    private EmojIconActions emojIconActions;
    private DirectionalLayout exitLayout;
    private DirectionalLayout rootView;
    private boolean isShow = false;
    private boolean isUser = false;
    private DirectionalLayout tempC;
    private int height;
    private int width;
    private boolean isScenedClick = true;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_statusColor));
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        initView();
        initClick();
    }

    private void initView() {
        checkBox = (Checkbox) findComponentById(ResourceTable.Id_main_check);
        PixelMapElement pixelMapElement = new PixelMapElement(getPixelMap(ResourceTable.Media_check_no));
        PixelMapElement pixelMapElement1 = new PixelMapElement(getPixelMap(ResourceTable.Media_check_yes));
        StateElement checkElement = new StateElement();
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, pixelMapElement1);
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, pixelMapElement);
        checkBox.setButtonElement(checkElement);
        checkBox.setChecked(false);
        tempC = (DirectionalLayout) findComponentById(ResourceTable.Id_c);

        emoji = (Image) findComponentById(ResourceTable.Id_emoji_btn);
        submit = (Image) findComponentById(ResourceTable.Id_submit_btn);
        textField = (EmojiconTextField) findComponentById(ResourceTable.Id_emojicon_text);
        textField1 = (EmojiconTextField) findComponentById(ResourceTable.Id_emojicon_text2);
        text = (EmojiconText) findComponentById(ResourceTable.Id_text);
        title = (Text) findComponentById(ResourceTable.Id_title);
        exitLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_exit_layout);
        rootView = (DirectionalLayout) findComponentById(ResourceTable.Id_root_view);
        title.setVisibility(Component.VISIBLE);
        textField.setTag("0");
        textField1.setTag("1");

        width = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes().width;
        height = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes().height;

        emojIconActions = new EmojIconActions(this, tempC, width, 850, textField, textField1);
        emojIconActions.setEmojiButton(emoji);
        emojIconActions.setEmojiButtonPixmap(ResourceTable.Media_ic_action_keyboard, ResourceTable.Media_smiley);
    }

    private void setCursorVisible(EmojiconTextField[] textFields) {
        for (EmojiconTextField textField : textFields) {
            if ("1".equals(textField.getTag())) {
                textField.setFocusable(Component.FOCUS_ENABLE);
                textField.setEnabled(true);
                textField.requestFocus();
                textField.setTextCursorVisible(true);
            }
        }
    }

    private void initClick() {
        textField1.setTouchEventListener((component, touchEvent) -> {
            textField1.setTag(1);
            textField.setTag(0);
            textField.clearFocus();
            if (checkBox.isChecked()) {
                textField1.setEnabled(false);
                textField1.setFocusable(Component.FOCUS_ENABLE);
                textField1.requestFocus();
                show();
            } else {
                if (!isShow) {
                    textField1.setEnabled(true);
                    textField1.setFocusable(Component.FOCUS_ENABLE);
                    textField1.requestFocus();
                    textField1.simulateClick();
                    title.setVisibility(Component.HIDE);
                    isShow = true;
                }
            }
            return false;
        });

        textField.setTouchEventListener((component, touchEvent) -> {
            textField.setTag(1);
            textField1.setTag(0);
            textField1.clearFocus();
            if (checkBox.isChecked()) {
                textField.setEnabled(false);
                textField.setFocusable(Component.FOCUS_ENABLE);
                textField.requestFocus();
                show();
            } else {
                textField.setEnabled(true);
                textField.setFocusable(Component.FOCUS_ENABLE);
                textField.requestFocus();
                textField.simulateClick();
                title.setVisibility(Component.HIDE);
                isShow = true;
            }
            return false;
        });

        emoji.setClickedListener(component -> {
            if (checkBox.isChecked()) {
                if (isUser) {
                    hide();
                } else {
                    show();
                }
            } else {
                if (isShow) {
                    textField1.clearFocus();
                    title.setVisibility(Component.VISIBLE);
                    emoji.setPixelMap(ResourceTable.Media_smiley);
                    isShow = false;
                } else {
                    title.setVisibility(Component.HIDE);
                    textField1.setFocusable(Component.FOCUS_ENABLE);
                    textField1.setEnabled(true);
                    textField1.requestFocus();
                    textField1.simulateClick();
                    emoji.setPixelMap(ResourceTable.Media_ic_action_keyboard);
                    isShow = true;
                }
            }
        });

        submit.setClickedListener(component -> {
            String text = textField1.getText();
            this.text.setText(text);
            if (isUser) {
                show();
            }
        });

        checkBox.setCheckedStateChangedListener((absButton, b) -> {
            if (b) {
                textField.clearFocus();
                textField1.clearFocus();
                setCursorVisible(new EmojiconTextField[]{textField, textField1});
            } else {
                hide();
            }
        });
    }

    private void show() {
        moveEdit(exitLayout, height - 1300);
        moveEdit(textField, height - 1430);
        moveEdit(rootView, height - 2000);
        moveEdit(tempC, height - 1100);
        title.setVisibility(Component.HIDE);
        emojIconActions.showPopup();
        emoji.setPixelMap(ResourceTable.Media_ic_action_keyboard);
        isUser = true;
    }

    private void hide() {
        resetEdit(exitLayout, height - 460);
        resetEdit(textField, height - 610);
        resetEdit(rootView, height - 1410);
        resetEdit(tempC, height);
        title.setVisibility(Component.VISIBLE);
        emojIconActions.hidePopup();
        emoji.setPixelMap(ResourceTable.Media_smiley);
        isUser = false;
        isShow = false;
    }

    private void moveEdit(Component component, int count) {
        AnimatorProperty animatorProperty = component.createAnimatorProperty();
        animatorProperty.moveFromY(height).moveToY(count).setDuration(0).start();
    }

    private void resetEdit(Component component, int count) {
        AnimatorProperty animatorProperty = component.createAnimatorProperty();
        animatorProperty.moveFromY(component.getContentPositionY()).moveToY(count).setDuration(0).start();
    }

    private PixelMap getPixelMap(int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            e.getMessage();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                e.getMessage();
            }
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onBackPressed() {
        isShow = false;
        if (isScenedClick) {
            isScenedClick = false;
            if (emojIconActions.getVisibility() == Component.VISIBLE) {
                hide();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isScenedClick = true;
                    }
                }, 1000);
            } else {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }
}